﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Lswweb.Domain;
using Lswweb.Model;
using Lswweb.IDAL;

namespace Lswweb.Controls
{
    public class OrderAdd : SkinnedWebControl
    {
        private TextBox tbxName;
        private TextBox tbxPhone;
        private TextBox tbxMobile;
        private TextBox tbxAddress;
        private TextBox tbxZip;
        private TextBox tbxRemark;
        private TextBox tbxCompanyName;
        private LogisticsRadioButtonList ltsType;
        private TreeCallBackDropDownList ddlCity;
        private CheckBox cbxLogisticsInsurance;

        private TextBox tbxEmail;
        private Control registerPanel;

        private IButtonControl btnConfirm;

        protected override void InitializeSkin(Control skin)
        {
            tbxName = (TextBox)skin.FindControl("tbxName");
            tbxPhone = (TextBox)skin.FindControl("tbxPhone");
            tbxMobile = (TextBox)skin.FindControl("tbxMobile");
            ddlCity = (TreeCallBackDropDownList)skin.FindControl("ddlCity");
            tbxAddress = (TextBox)skin.FindControl("tbxAddress");
            tbxZip = (TextBox)skin.FindControl("tbxZip");
            tbxRemark = (TextBox)skin.FindControl("tbxRemark");
            ltsType = (LogisticsRadioButtonList)skin.FindControl("ltsType");
            tbxEmail = (TextBox)skin.FindControl("tbxEmail");
            tbxCompanyName = (TextBox)skin.FindControl("tbxCompanyName");
            registerPanel = skin.FindControl("registerPanel");
            cbxLogisticsInsurance = skin.FindControl<CheckBox>("cbxLogisticsInsurance");
            btnConfirm = (IButtonControl)skin.FindControl("btnConfirm");

            btnConfirm.Click += new EventHandler(btnConfrim_Click);

            if (SiteContext.Current.Context.User.Identity.IsAuthenticated)
            {
                registerPanel.Visible = false;

                UserProfileInfo userProfile = SiteContext.Current.User != null ? SiteContext.Current.User.Profile : null;
                if (userProfile != null)
                {
                    tbxName.Text = userProfile.Name;
                    tbxPhone.Text = userProfile.Phone;
                    tbxMobile.Text = userProfile.Mobile;
                    if (userProfile.CityId > 0)
                        ddlCity.SelectValue = userProfile.CityId;

                    tbxAddress.Text = userProfile.Street;
                    tbxCompanyName.Text = userProfile.CompanyName;
                }
            }
            else
            {
                registerPanel.Visible = true;
            }
        }

        void btnConfrim_Click(object sender, EventArgs e)
        {
            OrderInfo info = Info;

            if (!SiteContext.Current.Context.User.Identity.IsAuthenticated)
            {
                IUsers userDAL = DALFactory.CreateUsers();
                UserInfo userInfo = new UserInfo();
                userInfo.LoginName = tbxEmail.Text.Trim();
                userInfo.Password = "123456";
                userInfo.PasswordAnswer = tbxEmail.Text.Trim();
                userInfo.PasswordQuestion = QuestionType.YoursSerialNumber;
                userInfo.Email = tbxEmail.Text.Trim();

                if (userDAL.IsHas(0, userInfo.LoginName))
                {
                    SiteContext.RenderMessage(ExceptionType.UserNameExists);
                }
                else
                {
                    userInfo.Password = Globals.MD5(userInfo.Password);
                    int userId = userDAL.Insert(userInfo);
                    if (userId > 0)
                    {
                        userInfo.Password = "123456";
                        Email.SendMail(userInfo);
                        FormsAuthentication.SetAuthCookie(userInfo.LoginName, false);

                        info.UserId = userId;
                    }
                }
            }
            else
            {
                info.UserId = SiteContext.Current.User.Id;
            }

            Orders.Insert(info,ddlCity.SelectValue);
        }

        private OrderInfo Info
        {
            get
            {
                EnsureChildControls();

                OrderInfo info = new OrderInfo();
                info.Address = tbxAddress.Text.Trim();
                info.Mobile = tbxMobile.Text.Trim();
                info.Name = tbxName.Text.Trim();
                info.Phone = tbxPhone.Text.Trim();
                info.Zip = tbxZip.Text.Trim();
                info.LtsType = ltsType.SelectedValue;

                info.DateCreated = DateTime.Now;
                info.Status = OrderStatus.WaitPayment;
                info.CompanyName = tbxCompanyName.Text.Trim();
                if (string.IsNullOrEmpty(info.CompanyName))
                {
                    info.CompanyName = " Null ";
                }
                info.Insurance = cbxLogisticsInsurance.Checked
                    ? this.Settings.LogisticsInsurance
                    : 0;

                info.Remark = tbxRemark.GetValue<string>();

                return info;
            }
        }

        protected override string DefaultSkinFilename
        {
            get { return "Order/OrderAdd.ascx"; }
        }
    }
}
